package com.cjh.lms.json;

import com.alibaba.fastjson.JSONObject;
import com.cjh.lms.exception.EncryptException;
import com.cjh.lms.tool.AES.AES;
import com.cjh.lms.tool.RSA.RSAUtil;
import com.cjh.lms.tool.String.StringUtil;

public class AppRequset {

	public final static Boolean isDecrypt = true;
	public static JSONObject getDataFromRSA(String dataString,String privateKey) throws EncryptException{
		JSONObject data = JSONObject.parseObject(dataString);	
		JSONObject unEncrypt = JSONObject.parseObject(data.getString("unEncrypt"));
		JSONObject unEncryptData = JSONObject.parseObject(unEncrypt.getString("data"));
		JSONObject result = new JSONObject();
		
		String encryptString = data.getString("encrypt");
		
		if(StringUtil.isEmptyOrNull(privateKey)){
			return null;
		}
		if(isDecrypt){
			try {
				encryptString = RSAUtil.decryptString(RSAUtil.getPrivateKey(privateKey), encryptString);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new EncryptException("10004");
			}
		}
		JSONObject encrypt = JSONObject.parseObject(encryptString);
		JSONObject encryptData = JSONObject.parseObject(encrypt.getString("data"));
		result.put("version", unEncrypt.getString("version"));
		result.put("username", unEncrypt.getString("username"));
		result.putAll(encryptData);
		result.putAll(unEncryptData);
		System.out.println("\n\n\n\n"+result.toJSONString()+"\n\n\n\n");
		return result;
	}
	
	public static JSONObject getDataFromAES(String dataString,String aesKey) throws EncryptException{
		JSONObject data = JSONObject.parseObject(dataString);		
		JSONObject unEncrypt = JSONObject.parseObject(data.getString("unEncrypt"));
		JSONObject unEncryptData = JSONObject.parseObject(unEncrypt.getString("data"));
		JSONObject result = new JSONObject();
		
		String encryptString = data.getString("encrypt");
	
		
		if(StringUtil.isEmptyOrNull(aesKey)){
			throw new EncryptException("10003");
		}
		if(isDecrypt){
			try {
				encryptString = AES.decrypt(aesKey, encryptString);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new EncryptException("10004");
			}
		}
		
		JSONObject encrypt = JSONObject.parseObject(encryptString);
		JSONObject encryptData = JSONObject.parseObject(encrypt.getString("data"));
		result.put("version", unEncrypt.getString("version"));
		result.put("username", unEncrypt.getString("username"));
		result.putAll(encryptData);
		result.putAll(unEncryptData);
			
		return result;
	}
	
	public static String getUsernameFromData(String dataString) throws EncryptException{
		try{
			JSONObject data = JSONObject.parseObject(dataString);
			JSONObject unEncrypt = JSONObject.parseObject(data.getString("unEncrypt"));
			String username = unEncrypt.getString("username");
			if(StringUtil.isEmptyOrNull(username)){
				throw new EncryptException("10005");
			}
			return username;
		}catch (Exception e) {
			// TODO: handle exception
			throw new EncryptException("10005");
		}
	}
	
}
